﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using NHibernate;
using System.Web.Security;
using ZLCMS.Business.DwzFrame;
using DotNet.Common.NHibernateUtil;
using DotNet.Common.Command;
using ZLCMS.Commands.Sys.Commands.User;

namespace ZLCMS.Web.SystemManage.Basics.RightConfig.User
{
    public partial class UserDetail : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int uid = 0;
                SysUserInfo user = null;
                string roleVal = "-1";
                IList<SysDepartmentInfo> deptList = null;
                if (Int32.TryParse(Request["uid"], out uid))
                {
                    using(ISession session = SessionFactoryHelper.CreateSession())
                    {
                        user = session.Get<SysUserInfo>(uid);
                        roleVal = session.CreateQuery("select RoleCode from UrUserRole where UserCode=:code").SetParameter("code", user.UserAccount).SetMaxResults(1).UniqueResult<string>();
                        if (string.IsNullOrEmpty(roleVal))
                        {
                            roleVal = "-1";
                        }
                    }
                    if (user != null)
                    {
                        account.ReadOnly = true;
                        account.Text = user.UserAccount;
                        phone.Text = user.Mobile;
                        name.Text = user.UserName;
                        email.Text = user.Email;
                        qq.Text = user.Qq;
                        ViewState["uid"] = uid;
                    }
                }
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    deptList = session.CreateQuery("from SysDepartmentInfo").List<SysDepartmentInfo>();
                }
                dept.DataSource = deptList;
                dept.DataValueField = "DeptCode";
                dept.DataTextField = "DeptName";
                dept.DataBind();
                if (user == null)
                {
                    user = new SysUserInfo();
                    password.Text = "123456";
                    user.Province = "-1";
                    user.City = "-1";
                    user.ValidStart = DateTime.Now;
                    user.ValidEnd = DateTime.Now.AddYears(1);
                }
                ProvinceCascadeCombox1.SelectedValue = user.Province;
                CityCascadeCombox1.SelectedValue = user.City;
                AreaCascadeCombox1.SelectedValue = user.Area;
                validend.Text = string.Format("{0:yyyy-MM-dd}", user.ValidEnd);
                validstart.Text = string.Format("{0:yyyy-MM-dd}", user.ValidStart);
                ParamKit.Bind("Roles", role, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, roleVal, "-1");
                dept.SelectedValue = user.DeptCode;
            }
            else
            {
                SaveUserParameter param = new SaveUserParameter();
                param.Id = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];
                param.QQ = qq.Text;
                param.Mobile = phone.Text;
                param.Name = name.Text;
                param.Status = "1";
                param.Province = ProvinceCascadeCombox1.SelectedValue;
                param.City = CityCascadeCombox1.SelectedValue;
                param.Area = AreaCascadeCombox1.SelectedValue;
                param.ValidStart = DateTime.Parse(validstart.Text);
                param.ValidEnd = DateTime.Parse(validend.Text);
                param.DeptCode = dept.SelectedValue;

                if (!string.IsNullOrEmpty(password.Text))
                {
                    param.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text, "MD5").ToLower();
                }
                param.Role = role.SelectedValue;
                param.UserAccount = account.Text;

                param.Email = email.Text;
                ICommandResult<SaveUserResult> result = new SaveUserCommand().Execute(param);

                    FrameHelper _framePage = new FrameHelper(this);
                    ReturnCode rc = new ReturnCode()
                    {
                        statusCode = result.Error ? 300 : 200,
                        message = result.ErrorMsg
                    };

                    rc.callbackType = "forward";
                    rc.forwardUrl = "/SystemManage/Basics/RightConfig/User/UserDetail.aspx?tabid=UserDetail";

                    if (Request["submit_op"] != "1" && !result.Error)
                    {
                        SysUserInfo user = result.Result.Data;
                        rc.forwardUrl += "&uid=" + user.Id;
                    }

                    _framePage.Alert(rc);
                
            }

        }
    }
}
